package com.qf.shopping.servlet.frontController;

import com.alibaba.fastjson.JSON;
import com.qf.shopping.common.Constants;
import com.qf.shopping.common.DisatpcherServlet;
import com.qf.shopping.entity.User;
import com.qf.shopping.service.IUserService;
import com.qf.shopping.service.impl.UserServiceImpl;
import com.qf.shopping.utils.ResultResponse;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**SSO:单点登录(登录，注册，注销，修改密码，账号激活)
 * @author 三旬
 * @version 1.0  2022/9/21
 */
@WebServlet("/SSOController/*")
public class SSOController extends DisatpcherServlet {
    //创建访问用户service层的对象
    IUserService userService = new UserServiceImpl();

    /**
     * 注册
     * @param request
     * @param response
     * @return
     */
    public String registerUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //接收注册的信息
        String regInfo = request.getReader().readLine();
        //封装
        User user = JSON.parseObject(regInfo, User.class);
        //调用业务层处理
        Map<String,Object> result = userService.registerUser(user);
        //把结果转成json响应
        return Constants.JSON+":"+JSON.toJSONString(result);
    }

    /**
     * 激活用户账号（异常：-1，未激活：0，正常：1）
     * @param request
     * @param response
     * @return
     */
    public void activateUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //接收前端传来的激活码
        String keyCode = request.getParameter("code");
        //验证激活码
        if(keyCode != null && !"".equals(keyCode)){
            //验证码存在，进行激活
            Integer result = userService.activateUser(keyCode);
            if (result > 0) {
                response.getWriter().println("激活成功，点击这里<a href = 'http://localhost:8080/login.jsp'>登录</a>");
            }
        }
    }

    /**
     * 前端登录
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    public String login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //接收异步登录的请求
        String json = request.getReader().readLine();
        //转成user对象
        User user = JSON.parseObject(json,User.class);
        System.out.println(user);
        //非空校验
        if(user.getUserName() == null || "".equals(user.getUserName())){
            //响应失败的消息
            return Constants.JSON+":"+ ResultResponse.Error("用户名不能为空！");
        }
        if(user.getPassWord() == null || "".equals(user.getPassWord())){
            //响应失败的消息
            return Constants.JSON+":"+ ResultResponse.Error("密码不能为空！");
        }
        //调用业务层处理
        Map<String,Object> map = userService.login(user);
        System.out.println(map);
        //接收map中的判断的key值code,获取value值
        Integer code = (Integer) map.get("code");
        if(code == 200){
            //登录验证成功,拿到结果将结果放到session 作用域中
            User userInfo = (User) map.get("user");
           request.getSession().setAttribute("user",userInfo);
        }
        //响应结果
        return Constants.JSON+":"+JSON.toJSONString(map);
    }
    public String nulllllllll(HttpServletRequest request, HttpServletResponse response){return null;}

}
